package t20211205;

/**
 * @Author: DengJia
 * @Date: 2021/12/5 14:30
 * @Description:
 */

class IncreImpl1 implements Increable {
    private int i = 0;
    @Override
    public void incre() {
        System.out.println(++i);
    }
}

class MyIncre implements Increable {
    @Override
    public void incre() {
        System.out.println("我的Incre~");
    }

    static void doMyIncre(MyIncre mi) {
        mi.incre();
    }
}

class IncreImpl2 extends MyIncre {
    private int i = 0;

    @Override
    public void incre() {
        super.incre();
        System.out.println(++i);
    }

    private class Closure implements Increable {
        @Override
        public void incre() {
            IncreImpl2.this.incre();
        }
    }

    Increable getCallbackReference() {
        return new Closure();
    }
}

class Caller {
    private Increable cbRef;

    Caller(Increable cbRef) {
        this.cbRef = cbRef;
    }

    void go() {
        cbRef.incre();
    }
}

public class Callbacks {
    public static void main(String[] args) {
        IncreImpl1 c1 = new IncreImpl1();
        IncreImpl2 c2 = new IncreImpl2();

        MyIncre.doMyIncre(c2);
        Caller caller1 = new Caller(c1);
        Caller caller2 = new Caller(c2.getCallbackReference());
        caller1.go();
        caller1.go();
        caller2.go();
        caller2.go();
    }
}
